#!/bin/bash

cmd_name="$1"
if [ -z "$cmd_name" ] ; then
	cat << EOF
	usage: $0 command
EOF
	exit 1
fi

if [ -z "$lc_has_init" ]; then

	export PATH=$PATH:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:~/bin
	#echo "init lc env..."
	PRG="$0"
	while [ -h "$PRG" ]; do
		ls=`ls -ld "$PRG"`
		link=`expr "$ls" : '.*-> \(.*\)$'`
		if expr "$link" : '/.*' > /dev/null; then
			PRG="$link"
		else
			PRG=`dirname "$PRG"`/"$link"
		fi  
	done

	# 基本配置信息
	export lc_path=`dirname $PRG`

	export lc_config_path=$lc_path/config
	export lc_lib_path=$lc_path/lib
	export lc_bin_path=$lc_path/bin

	export PATH=$PATH:$lc_bin_path

	#echo "has inited lc env."
	export lc_has_init=1

fi

for mylib in `find "$lc_lib_path" -type f -name "lc_*_lib.sh"`
do
	#echo "source $mylib"
	. $mylib
done

shift
if ! check_cmd_exists "$cmd_name" ; then
	if check_cmd_exists "lc_$cmd_name.sh" ; then
		cmd_name=lc_$cmd_name.sh
	elif check_cmd_exists "$cmd_name.sh" ; then
		cmd_name=$cmd_name.sh
	elif check_is_fun $cmd_name ; then
		$cmd_name $*
		exit $?
	else
		echo 程序$cmd_name没有找到，执行失败
		exit 1
	fi	
fi
$cmd_name $*
exit $?
